package com.kuson.springcloud.service.impl;

import com.kuson.springcloud.domain.Order;
import com.kuson.springcloud.mapper.OrderMapper;
import com.kuson.springcloud.service.OrderService;
import com.kuson.springcloud.service.rpc.AccountService;
import com.kuson.springcloud.service.rpc.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单模块逻辑层
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * 创建订单
     *   创建订单的过程，减库存-->减余额-->更新订单状态
     * @param order
     */
    @Override
    public void create(Order order) {
        log.info("----->开始新建订单");
        //新建订单
        orderMapper.create(order);

        //扣减库存
        log.info("----->订单微服务开始调用库存，做扣减Count");
       storageService.decrease(order.getProductId(),Long.valueOf(order.getCount()));
        log.info("----->订单微服务开始调用库存，做扣减end");

        //扣减账户
        log.info("----->订单微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->订单微服务开始调用账户，做扣减end");


        //修改订单状态，从零到1代表已经完成
        log.info("----->修改订单状态开始");
        orderMapper.update(order.getUserId(),0l);
        log.info("----->修改订单状态结束");

        log.info("----->下订单结束了");

    }

    /**
     * 更新订单的状态更新订单的状态
     * @param userId
     * @param status
     * @return
     */
    @Override
    public int updataOrderState(Long userId, Long status) {
        return orderMapper.update(userId,status);
    }
}
